﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.Core.Model;
using Wolfy.NetDisk.Core.Repository;

namespace Wolfy.NetDisk.Data
{
    public class UnitRepository : IUnitRepository
    {
        private readonly DbContext db;
        public UnitRepository(IDbContextFactory factory)
        {
            this.db = factory.GetContext();
        }
        public T Insert<T>(T entity) where T : Entity, new()
        {
            this.db.Set<T>().Add(entity);
            return entity;
        }

        public void Save()
        {
            this.db.SaveChanges();
        }

        public T Get<T>(int id) where T : Entity
        {
            return this.db.Set<T>().Find(id);
        }

        public IEnumerable<T> GetAll<T>(bool showDeleted) where T : DeleteEntity
        {
            if (showDeleted)
            {
                return this.db.Set<T>();
            }
            return this.db.Set<T>().Where(O => !O.IsDeleted);
        }
    }
}
